package com.ruoyi.backend.controller;

import com.ruoyi.backend.domain.Shop;
import com.ruoyi.backend.domain.ShopService;
import com.ruoyi.backend.service.IShopService;
import com.ruoyi.backend.service.IShopServiceService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 店铺Controller
 * 
 * @author wjr
 * @date 2025-09-23
 */
@RestController
@RequestMapping("/backend/t_shop")
@Api(tags = "店铺")
public class ShopController extends BaseController
{
    @Autowired
    private IShopService shopService;
    @Autowired
    private IShopServiceService shopServiceService;

    /**
     * 查询店铺列表
     */
    @PreAuthorize("@ss.hasPermi('backend:t_shop:list')")
    @GetMapping("/list")
    @ApiOperation("查询店铺列表")
    public TableDataInfo list(Shop shop)
    {
        startPage();
        List<Shop> list = shopService.selectShopList(shop);
        return getDataTable(list);
    }

    /**
     * 获取店铺详细信息
     */
    @PreAuthorize("@ss.hasPermi('backend:t_shop:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取店铺详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        AjaxResult result = new AjaxResult();
        result.put(AjaxResult.DATA_TAG, shopService.selectShopById(id));
        result.put(AjaxResult.CODE_TAG, HttpStatus.SUCCESS);
        result.put(AjaxResult.MSG_TAG, "");

        //查询店铺服务信息
        ShopService check = new ShopService();
        check.setShopId(id);
        List<ShopService> shopServiceList = shopServiceService.selectShopServiceList(check);
        if (shopServiceList != null && shopServiceList.size() > 0) {
            //筛选店铺服务信息
            result.put("serviceIds", shopServiceList.stream().map(ShopService::getServiceId).collect(Collectors.toList()));
        }

        return result;
    }

    /**
     * 新增店铺
     */
    @PreAuthorize("@ss.hasPermi('backend:t_shop:add')")
    @Log(title = "店铺", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增店铺")
    public AjaxResult add(@RequestBody Shop shop)
    {
        return toAjax(shopService.insertShop(shop));
    }

    /**
     * 修改店铺
     */
    @PreAuthorize("@ss.hasPermi('backend:t_shop:edit')")
    @Log(title = "店铺", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改店铺")
    public AjaxResult edit(@RequestBody Shop shop)
    {
        return toAjax(shopService.updateShop(shop));
    }

    /**
     * 删除店铺
     */
    @PreAuthorize("@ss.hasPermi('backend:t_shop:remove')")
    @Log(title = "店铺", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除店铺")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(shopService.deleteShopByIds(ids));
    }

    /**
     * 获取当前店铺详细信息
     */
    @PreAuthorize("@ss.hasPermi('backend:t_shop:query')")
    @GetMapping(value = "/getCurrentShop")
    @ApiOperation("获取当前店铺详细信息")
    public AjaxResult getCurrentShop()
    {
        Integer shopId = SecurityUtils.getLoginUser().getUser().getShopId();
        if (shopId == 0) {
            throw new ServiceException("当前用户没有关联店铺");
        }
        return success(shopService.selectShopById(shopId));
    }

    /**
     * 导出店铺列表
     */
    /*@PreAuthorize("@ss.hasPermi('backend:t_shop:export')")
    @Log(title = "店铺", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Shop shop)
    {
        List<Shop> list = shopService.selectShopList(shop);
        ExcelUtil<Shop> util = new ExcelUtil<Shop>(Shop.class);
        util.exportExcel(response, list, "店铺数据");
    }*/

}
